<template>
  <el-scrollbar height="100%">
    <div class="nav-menu">
      <div class="logo">
        <!-- template使用别名 加上 ~ ,vue-loader对其进行解析 -->
        <img class="img" src="~@/assets/img/logo.svg" alt="这是logo" />
        <span v-if="!isCollapse" class="title">Fhup-cms</span>
      </div>
      <el-menu
        :default-active="checkedmenuId"
        class="el-menu-vertical"
        :collapse="isCollapse"
        active-text-color="#0a60bd"
        background-color="#0c2135"
        text-color="#b7bdc3"
        :unique-opened="false"
      >
        <template v-for="item in usermenus" :key="item.id">
          <!-- 二级菜单 -->
          <template v-if="item.type === 1">
            <!-- 二级菜单可以展开的标题 -->
            <el-sub-menu class="el-sub-menu" :index="item.id + ''">
              <template #title>
                <el-icon v-if="item.icon == 'el-icon-monitor'">
                  <Money />
                </el-icon>
                <el-icon v-else-if="item.icon == 'el-icon-setting'">
                  <Setting />
                </el-icon>
                <el-icon v-else-if="item.icon == 'el-icon-goods'">
                  <Goods />
                </el-icon>
                <el-icon v-else-if="item.icon == 'el-icon-chat-line-round'">
                  <ChatLineRound />
                </el-icon>
                <span>
                  {{ item.name }}
                </span>
              </template>
              <!-- 遍历标题里面的item -->
              <template v-for="subitem in item.children" :key="subitem.id">
                <el-menu-item
                  class="el-menu-item"
                  :index="subitem.id + ''"
                  @click="menusClick(subitem)"
                >
                  <i v-if="subitem.icon"></i>
                  <span>{{ subitem.name }}</span>
                </el-menu-item>
              </template>
            </el-sub-menu>
          </template>
          <!-- 一级菜单 -->
          <template v-else-if="item.type === 2">
            <el-menu-item :index="item.id + ''">
              <i v-if="item.icon"></i>
              <span>{{ item.name }}</span>
            </el-menu-item>
          </template>
        </template>
      </el-menu>
    </div>
  </el-scrollbar>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
// vuex对ts支持没有pinia好
// 自己封装的useStore
import { useStore } from '@/store'
import { useRouter, useRoute } from 'vue-router'
import { pathToMenus } from '@/utils/MenusToRoutes'
export default defineComponent({
  props: {
    isCollapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    // store
    const store = useStore()
    const usermenus = computed(() => {
      return store.state.login.userMenus
    })
    // router(进行push...)
    const router = useRouter()
    // route(拿到当前的路由)
    const route = useRoute()
    const currentpath = route.path
    // 注意 usermenus 是computedref对象,要加 .value
    const menu = pathToMenus(usermenus.value, currentpath)
    const checkedmenuId = ref(menu.id + '')

    const menusClick = (menu: any) => {
      router.push(menu.url ?? '/notfound')
    }

    return {
      usermenus,
      menusClick,
      checkedmenuId
    }
  }
})
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 13px;
      font-weight: 700;
      color: white;
    }
  }
  .el-menu-vertical {
    border-right: none;
    // 目录
    .el-sub-menu {
      .el-sub-menu__title {
        span {
          font-size: 13px;
        }
      }
      background-color: #001529 !important;
      // 二级菜单 ( 默认背景 )
      .el-menu-item {
        font-size: 13px;
        padding-left: 53px !important;
        background-color: #0c2135 !important;
      }
    }
    // vue样式穿透
    :deep(.el-submenu__title) {
      background-color: #001529 !important;
    }

    // hover 高亮
    .el-menu-item:hover {
      color: #fff !important; // 菜单
    }

    .el-menu-item.is-active {
      color: #fff !important;
      background-color: #0a60bd !important;
    }
  }
}
// 解决aside伸缩布局样式
.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>
